﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProcurementManagement.Applcation.Command.ProcureApplyCommand;
using ProcurementManagement.Applcation.Command.ProcureEnquiryCommand;
using ProcurementManagement.Applcation.Command.PurchaseContractsCommand;
using ProcurementManagement.Domain.Dtos;
using ProcurementManagement.Domain.ProcurementDomain;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace ProcurementManagement.Controllers
{
    /// <summary>
    /// 采购申请表
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class ProcureApplyController : ControllerBase
    {
        private readonly IMediator _mediator;

        public ProcureApplyController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 新增采购申请
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddProcureApply(ProcureApplyCreateDto command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id= sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.AddName = name;
                command.AddTime = DateTime.Now;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                command.User_Id = int.Parse(userId);
                result.Data = await _mediator.Send(new ProcureApplyCreateCommand { dto=command});
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示采购申请列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ProcureApplyPage>> GetProcureApply([FromQuery]ProcureApplySelectCommand command)
        {
            ApiResult<ProcureApplyPage> result = new ApiResult<ProcureApplyPage>();
            try
            {
                //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                //command.Office_Id = sid;
                //var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                //command.AddName = name;
                //command.AddTime = DateTime.Now;
                ////获取用户ID
                //var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                //command.User_Id = int.Parse(userId);
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除采购申请
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelProcureApply(ProcureApplyDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                //command.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.DelName = name;
                //command.AddTime = DateTime.Now;
                ////获取用户ID
                //var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                //command.User_Id = int.Parse(userId);
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填采购申请
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ProcureApplyFanDto>> FanProcureApply([FromQuery] ProcureApplyIdFanCommand command)
        {
            ApiResult<ProcureApplyFanDto> result = new ApiResult<ProcureApplyFanDto>();
            try
            {
                //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                //command.Office_Id = sid;
                //var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                //command.AddName = name;
                //command.AddTime = DateTime.Now;
                ////获取用户ID
                //var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                //command.User_Id = int.Parse(userId);
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改采购申请
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdProcureApply(ProcureApplyUpdDto command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                //command.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.UpdName = name;
                command.UpdTime = DateTime.Now;
                ////获取用户ID
                //var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                //command.User_Id = int.Parse(userId);
                result.Data = await _mediator.Send(new ProcureApplyUpdCommand { dto = command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 入库申请添加时显示产品是采购之后的产品中介者
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<ProcureApplySelectLFDto>>> ProcureEnquirySelectLuFang()
        {
            ApiResult<List<ProcureApplySelectLFDto>> result = new ApiResult<List<ProcureApplySelectLFDto>>();
            try
            {
                result.Data = await _mediator.Send(new ProcureApplyLuFangCommand());
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }



    }
}
